package com.enuo.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.NotBlank;

import com.enuo.CommonAttributes;
import com.enuo.utils.Utils;

/**
 * @ClassName: Menu
 * @Description: TODO(Entity - 菜单)
 * @author Charles
 * @date 2017年11月21日 下午1:38:20
 * @version 1.0
 */
@Entity
@Table(name = "menu")
public class Menu extends OrderEntity
{
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 1258597443607754648L;

    private String name;

    private String icon;

    private List<MenuItem> menuItems = new ArrayList<MenuItem>();

    @NotBlank
    @Column(name = "name", unique = true, nullable = false)
    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @NotBlank
    @Column(name = "icon")
    public String getIcon()
    {
        return this.icon;
    }

    public void setIcon(String icon)
    {
        if (!Utils.isEmpty(icon))
        {
            if (icon.startsWith(CommonAttributes.SITE_URL))
            {
                icon = icon.substring(CommonAttributes.SITE_URL.length(), icon.length());
            }
        }

        this.icon = icon;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "menu")
    @OrderBy("order asc")
    public List<MenuItem> getMenuItems()
    {
        return this.menuItems;
    }

    public void setMenuItems(List<MenuItem> menuItems)
    {
        this.menuItems = menuItems;
    }

    @Transient
    public String getFullIcon()
    {
        if (!Utils.isEmpty(icon))
        {
            return CommonAttributes.SITE_URL + icon;
        }

        return null;
    }
}